package com.xiaoyao.novel.action.page;

import java.util.List;

import com.xiaoyao.novel.common.Pagin;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.service.BookService;
import com.xiaoyao.novel.util.HtmlStatic;

public class BookAction extends NovelAction {
	private static final long serialVersionUID = 1L;
	Book book;
	Pagin pagin = new Pagin();

	// ////////////////////////////////////////////
	// 页面
	public String book() throws Exception {
		getConfig();
		listCategory();
		getFooter();
		getRecommend(8, 0);
		getHot(20);
		// 书籍
		book = bookService.getById(book.getBookId());
		// 目录
		pagin = new Pagin();
		pagin.setPageSize(10000);
		pagin.getSearchParam().put("orderBy", " cid asc ");
		pagin.getSearchParam().put("bookId", book.getBookId() + "");
		List<Chapter> chapterList = chapterService.search(pagin);
		// if (chapterList.size() > 0) {
		// book.setNewChapter(chapterList.get(0));
		// }
		book.setList(chapterList);
		request.setAttribute("book", book);
		return "book";
	}

	// ////////////////////////////////////////////
	// 后台
	@Override
	public String execute() throws Exception {
		return search();
	}

	public String search() throws Exception {
		pagin.getSearchParam().put("orderBy", " isRecommend desc , updateTime desc  ");
		// String bookName = pagin.getSearchParam().get("bookName");
		// if (!ClassUtil.isEmpty(bookName)) {
		// bookName=new String(bookName.getBytes("ISO8859-1"),"UTF-8");
		// pagin.getSearchParam().put("bookName", bookName) ;
		// }
		bookService.search(pagin);
		request.setAttribute("pagin", pagin);
		return SUCCESS;
	}

	public String deleteBook() throws Exception {
		bookService.delete(book.getBookId());
		chapterService.deleteBook(book.bookId);// 删除相关章节数据.
		HtmlStatic.staticIndex();// 更新首页
		request.setAttribute("msg", "删除书籍" + book.bookId + ",删除章节,静态化首页 成功!");
		return execute();
	}

	public String toAddBook() throws Exception {
		List<Category> categoryList = categoryService.getAll();
		request.setAttribute("categoryList", categoryList);
		return "showAdd";
	}

	public String addBook() throws Exception {
		bookService.insert(book);
		return execute();
	}

	public String showModify() throws Exception {
		book = bookService.getById(book.getBookId());
		List<Category> categoryList = categoryService.getAll();
		request.setAttribute("book", book);
		request.setAttribute("categoryList", categoryList);
		return "showModify";
	}

	public String modifyBook() throws Exception {
		logger.info("update:" + book);
		bookService.update(book);
		return execute();
	}

	public String over() throws Exception {
		int isOver = book.getIsOver();
		book = bookService.getById(book.getBookId());
		book.setIsOver(isOver);
		bookService.update(book);
		return execute();
	}

	public String recommend() throws Exception {
		book = bookService.getById(book.getBookId());
		book.isRecommend = 1;
		bookService.update(book);
		return execute();
	}

	public String cancelRecommend() throws Exception {
		book = bookService.getById(book.getBookId());
		book.isRecommend = 0;
		bookService.update(book);
		return execute();
	}

	// /////////////////////////////////

	public BookService getBookService() {
		return bookService;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public Pagin getPagin() {
		return pagin;
	}

	public void setPagin(Pagin pagin) {
		this.pagin = pagin;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
